<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="问答分类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="categoryId">
              <j-dict-select-tag
                type="list"
                v-model="model.categoryId"
                dictCode="ht_forums_category,name,id"
                placeholder="请选择问答分类"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="标题" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入关键字"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="描述" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="description">
              <j-editor v-model="model.description" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'
import { mapState, mapActions } from 'vuex'

export default {
  name: 'HTForumsQuestionForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {
        auditStatus: 2,
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        categoryId: [{ required: true, message: '请输入问答分类' }],
        name: [{ required: true, message: '请输入关键字!' }],
        description: [{ required: true, message: '请输入描述!' }],
      },
      url: {
        add: '/forums/hTForumsQuestion/add',
        edit: '/forums/hTForumsQuestion/edit',
        queryById: '/forums/hTForumsQuestion/queryById',
      },
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    ...mapActions('forumsQuestion', ['submitQuestionAction']),

    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },

    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          that
            .submitQuestionAction(that.model)
            .then((response) => {
              if (response.success) {
                that.model = {}
                that.$message.success('提交成功')
                that.$emit('ok')
              } else {
                that.$message.error(response.message)
              }
            })
            .catch((error) => {
              that.$message.error(error.message)
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>

